import com.sun.source.tree.Tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 12629
 * @Description：
 */
public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //public TreeNode root;

    /**
     * 创建一棵二叉树 创建成功后 返回根节点
     * @return
     */
    public TreeNode createTree1() {
        TreeNode A = new TreeNode('3');
        TreeNode B = new TreeNode('4');
        TreeNode C = new TreeNode('5');
        TreeNode D = new TreeNode('1');
        TreeNode E = new TreeNode('2');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;


        return A;
    }
    public TreeNode createTree2() {
        TreeNode A = new TreeNode('4');
        TreeNode B = new TreeNode('1');
        TreeNode C = new TreeNode('2');



        A.left = B;
        A.right = C;


        return A;
    }
    // 前序遍历
    void preOrder(TreeNode root){
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }


    /*List<Integer> ret = new ArrayList<>();
     *//**
     * 把 前序遍历的结果 存储到List 当中
     * @param root
     * @return
     *//*
    public List<Integer> preorderTraversal(TreeNode root) {
        if(root == null) {
            return null;
        }
        ret.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return ret;
    }*/
    /*public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null) {
            return list;
        }
        list.add(root.val);
        List<Integer> leftTree = preorderTraversal(root.left);
        list.addAll(leftTree);
        List<Integer> rightTree = preorderTraversal(root.right);
        list.addAll(rightTree);
        return list;
    }*/
    // 中序遍历  -》 左根右
    void inOrder(TreeNode root){
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }


    // 后序遍历  -》 左右根
    void postOrder(TreeNode root){
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val+" ");
    }
    public int getLeafNodeCount(TreeNode root){
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }
    public int getKLveNodeCount(TreeNode root,int k){
        if(root == null){
            return 0;
        }
        if(k == 1){
            return 1;
        }
        return getKLveNodeCount(root.left,k-1) + getKLveNodeCount(root.right,k-1);
    }
    public TreeNode find(TreeNode root,char val){
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        TreeNode ret1 = find(root.left,val);
        if(ret1 != null){
            return ret1;
        }
        TreeNode ret2 = find(root.right,val);
        if(ret2 != null){
            return ret2;
        }
        return null;
    }
    //查找是否是相同的树
    public boolean isSymmetric(TreeNode root) {
        if(root == null){
            return true;
        }
        return is(root.left, root.right);
    }
    public boolean is(TreeNode left, TreeNode right){
        if(left != null && right == null || left == null && right != null){
            return false;
        }
        if(left == null && right == null){
            return true;
        }
        if(left.val != right.val){
            return false;
        }

        return is(left.right,right.left) && is(left.left,right.right);

    }
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null || subRoot == null){
            return false;
        }
        if(root != null && subRoot == null || root == null && subRoot != null){
            return false;
        }
        if(root.val != subRoot.val){
            return false;
        }
        return isSubtree(root.left,subRoot.left) && isSubtree(root.right,subRoot.right);
    }
}